import axios from "axios";
import { ElMessage } from "element-plus";

const service = axios.create({
    baseURL: 'http://127.0.0.1:3000',
    timeout: 5000,
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json',
        // 添加其他的头部信息
    }
});

//axios请求拦截器
service.interceptors.request.use(
    (config) => {
        if (sessionStorage.getItem("token")) {
            config.headers.Authorization = window.sessionStorage.getItem("token");
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

//axios响应拦截器
service.interceptors.response.use(
    (res) => {
        if (res.data.code === 401) {
            ElMessage.error('身份认证过期，请登录')
        }
        return res
    },
    (error) => {
        return Promise.reject(error);
    }
);

export default service;
